package cn.harry.modular.cp.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.modular.cp.service.EvaluationContentService;
import cn.harry.modular.cp.domain.EvaluationContent;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 评测内容前端控制层
 *
 * @author harry
 * @公众号 Harry技术
 */
@Tag(name = "评测内容接口")
@RestController
@RequestMapping("/evaluationContent")
@RequiredArgsConstructor
public class EvaluationContentController {

    private final EvaluationContentService evaluationContentService;

    /**
     * 分页查询列表
     */
    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
    public R<IPage<EvaluationContent>> page(@ParameterObject Page<EvaluationContent> page, @ParameterObject EvaluationContent entity) {
        LambdaQueryWrapper<EvaluationContent> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EvaluationContent::getEvaluationId, entity.getEvaluationId());
        return R.success(evaluationContentService.page(page, wrapper));
    }

    /**
     * 根据id获取详情
     */
    @Operation(summary = "根据id获取详情")
    @GetMapping(value = "/{id}")
    public R<EvaluationContent> getById(@PathVariable Long id) {
        return R.success(evaluationContentService.getById(id));
    }

    /**
     * 新增
     */
    @Operation(summary = "新增")
    @SysLog(title = "evaluation_content", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> save(@RequestBody EvaluationContent entity) {
        // 评测内容总分 = 思想信念表现+ 道德品质表现+ 学习能力表现+ 工作能力表现+ 身心素质表现+ 综合素质表现
        Integer scoreSum = sumScore(entity);
        entity.setScoreSum(scoreSum);
        return evaluationContentService.save(entity) ? R.success() : R.failed();
    }

    private Integer sumScore(EvaluationContent entity) {
        Integer zhengzhi1 = entity.getZhengzhi1();
        Integer zhengzhi2 = entity.getZhengzhi2();
        Integer zhengzhi3 = entity.getZhengzhi3();
        Integer zhiye1 = entity.getZhiye1();
        Integer zhiye2 = entity.getZhiye2();
        Integer zhiye3 = entity.getZhiye3();
        Integer zuofeng1 = entity.getZuofeng1();
        Integer zuofeng2 = entity.getZuofeng2();
        Integer zuofeng3 = entity.getZuofeng3();
        Integer lianjie1 = entity.getLianjie1();
        Integer lianjie2 = entity.getLianjie2();
        Integer lianjie3 = entity.getLianjie3();
        Integer tuandui1 = entity.getTuandui1();
        Integer tuandui2 = entity.getTuandui2();
        Integer tuandui3 = entity.getTuandui3();
        return zhengzhi1 + zhengzhi2 + zhengzhi3 + zhiye1 + zhiye2 + zhiye3 + zuofeng1 + zuofeng2 + zuofeng3 + lianjie1 + lianjie2 + lianjie3 + tuandui1 + tuandui2 + tuandui3;
    }

    /**
     * 更新
     */
    @Operation(summary = "更新")
    @SysLog(title = "evaluation_content", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Integer> update(@RequestBody EvaluationContent entity) {
        return evaluationContentService.updateById(entity) ? R.success() : R.failed();
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @SysLog(title = "evaluation_content", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable Long[] ids) {
        return evaluationContentService.removeBatchByIds(Arrays.asList(ids)) ? R.success() : R.failed();
    }
}
